using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Sheep_Breeding.ErrorCode;
using Sheep_Breeding.Read.Api.Dtos;
using Sheep_Breeding.Read.Api.OperationServices.IServices;

namespace Sheep_Breeding.Read.Api.Controllers
{
    /// <summary>
    /// 期初管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PhaseBeginController : ControllerBase
    {
        private readonly IPhaseBeginServices phaseBeginServices;
        private readonly ILogger<PhaseBeginController> logger;

        public PhaseBeginController(IPhaseBeginServices phaseBeginServices, ILogger<PhaseBeginController> logger)
        {
            this.phaseBeginServices = phaseBeginServices;
            this.logger = logger;
        }

        /// <summary>
        /// 获取期初列表
        /// </summary>
        /// <param name="queryDto">查询条件</param>
        /// <returns>分页期初列表</returns>
        [HttpGet]
        // [Authorize] // 暂时注释掉授权要求
        public async Task<ApiResult<ApiPaging<List<PhaseBeginDto>>>> GetList([FromQuery] PhaseBeginQueryDto queryDto)
        {
            try
            {
                logger.LogInformation($"收到获取期初列表请求，查询条件：耳号={queryDto.EarNumber}，羊只类型={queryDto.SheepType}，页码={queryDto.PageIndex}，每页数量={queryDto.PageSize}");
                return await phaseBeginServices.GetList(queryDto);
            }
            catch (Exception ex)
            {
                logger.LogError($"获取期初列表接口异常: {ex.Message}");
                var result = new ApiResult<ApiPaging<List<PhaseBeginDto>>>();
                result.msg = "获取期初列表过程中发生异常";
                result.code = ApiREunm.Fail;
                return result;
            }
        }
    }
} 